﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Shared.Method
{
    public class ProcessClass
    {
        #region 开启进程
        public static bool StartProcess(string processName, string processFolder)
        {
            try
            {
                if (processName.Contains(".exe") == false)//如果没有.exe结尾则加上
                {
                    processName += ".exe";
                }

                Process p = new Process();
                string processPath = Path.Combine(processFolder, processName);
                if (File.Exists(processPath))
                {
                    p = Process.Start(processPath);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }


        public static bool StartProcess(string processName, string processFolder, string[] args)
        {
            if (args.Length == 0) return StartProcess(processName, processFolder);
            try
            {
                if (processName.Contains(".exe") == false)//如果没有.exe结尾则加上
                {
                    processName += ".exe";
                }

                string processPath = Path.Combine(processFolder, processName);
                if (File.Exists(processPath))
                {
                    ProcessStartInfo processInfo = new ProcessStartInfo();
                    processInfo.FileName = processPath;
                    //processInfo.CreateNoWindow = false;
                    processInfo.Arguments = null;
                    for (int i = 0; i < args.Length; i++)//添加参数
                    {
                        processInfo.Arguments += args[i] + " ";
                    }
                    Process.Start(processInfo);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 执行CMD指令，并返回值
        /// </summary>
        /// <param name="processName">指令名</param>
        /// <param name="args">指令参数</param>
        /// <param name="outPutMsg">返回信息</param>
        /// <returns>执行结果</returns>
        public static bool StartProcess(string processName, out string outPutMsg, string[] args = null, string processFolder = null)
        {
            outPutMsg = string.Empty;
            try
            {
                //去掉后缀
                if (processName.Contains(".")) processName = processName.Substring(0, processName.IndexOf("."));
                //初始化
                KillProcess(processName);

                string processPath = processName + ".exe";

                if (string.IsNullOrEmpty(processFolder) == false)
                {
                    processPath = Path.Combine(processFolder, processName, ".exe");
                }

                Process p = new Process();
                p.StartInfo.FileName = processPath;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                for (int i = 0; args != null && args.Length > 0 && i < args.Length; i++)//添加参数
                {
                    p.StartInfo.Arguments += args[i] + " ";
                }


                if (p.Start())
                {
                    p.StandardInput.AutoFlush = true;

                    var waitRes = p.WaitForExit(45000);
                    if (waitRes == false)
                    {
                        outPutMsg = "等待45秒，执行指令失败";
                        return false;
                    }

                    outPutMsg = string.Format("{0}\n执行指令完成", p.StandardOutput.ReadToEnd());
                    //AppendRichTextBox(output);

                    p.Close();
                }

                return true;//返回输出的字符串


            }
            catch (Exception ex)
            {
                outPutMsg = ex.ToString();
                return false;
            }
        }

        #endregion

        #region 结束进程
        public static bool KillProcess(string processName)
        {
            try
            {
                var pro = Process.GetProcessesByName(processName);
                for (int i = 0; pro.Length > 0 && i < pro.Length; i++)
                {
                    pro[i].Kill();
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool KillProcess(int processId)
        {
            try
            {
                var pro = Process.GetProcessById(processId);
                pro.Kill();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool KillProcess()
        {
            try
            {
                var pro = Process.GetCurrentProcess();
                pro.Kill();
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region 清除缓存

        public static void ClearArp()
        {
            string cmdStr = "arp -d & exit";
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;

            if (p.Start())
            {
                p.StandardInput.WriteLine(cmdStr);
                p.StandardInput.AutoFlush = true;
                p.WaitForExit();

                p.Close();
            }
        }

        #endregion
    }
}
